package com.example.controller;

import cn.hutool.core.lang.Dict;
import com.example.common.Result;
import com.example.controller.domain.SearchRequest;
import com.example.entity.Works;
import com.example.recommend.RecommendService;
import com.example.service.WorksService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 文艺作品前端操作接口
 **/
@RestController
@RequestMapping("/works")
public class WorksController {

    @Resource
    private WorksService worksService;
    @Resource
    private RecommendService recommendService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Works works) {
        worksService.add(works);
        return Result.success("新增文艺作品成功");
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        worksService.deleteById(id);
        return Result.success("删除文艺作品成功");
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        worksService.deleteBatch(ids);
        return Result.success("删除文艺作品成功");
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Works works) {
        worksService.updateById(works);
        return Result.success("更新文艺作品成功");
    }

    /**
     * 更新浏览量
     */
    @PutMapping("/updateCount/{id}")
    public Result updateCount(@PathVariable Integer id){
        worksService.updateCount(id);
        return Result.success("更新浏览量成功");
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Works works = worksService.selectById(id);
        return Result.success("查询文艺作品成功",works);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(Works works) {
        List<Works> list = worksService.selectAll(works);
        return Result.success("查询文艺作品列表成功",list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(Works works,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Works> page = worksService.selectPage(works, pageNum, pageSize);
        return Result.success("分页查询文艺作品列表成功",page);
    }
    /**
     * 随机推荐
     */
    @GetMapping("/selectRecommend")
    public Result selectRecommend(){
        List<Works> list = worksService.selectRecommend();
        return Result.success("随机推荐文艺作品成功",list);
    }

    /**
     * 我的收藏
     */
    @GetMapping("/selectCollect")
    public Result selectCollect(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "6") Integer pageSize){
        return Result.success("查询我收藏的文艺作品成功",worksService.selectCollect(pageNum,pageSize));
    }
    @GetMapping("/selectCount")
    public Result selectCount() {
        List<Dict> list = worksService.selectCount();  //  [{ date: '1-8', value: 10 } ]
        return Result.success(list);
    }

    @GetMapping("/selectCategoryCount")
    public Result selectCategoryCount() {
        List<Dict> list = worksService.selectCategoryCount();
        return Result.success("查询文艺作品统计数据成功",list);
    }

    @GetMapping("/listBySearchKeyword")
    public Result queryForListBySearchKeyword(SearchRequest searchRequest,
                                              @RequestParam(defaultValue = "1") Integer pageNum,
                                              @RequestParam(defaultValue = "10")Integer pageSize){
        return Result.success("关键字搜索文艺作品成功",worksService.queryForListBySearchKeyword(searchRequest,pageNum,pageSize));
    }

    @GetMapping("/listBySearchKeywordCount")
    public Result queryForListBySearchKeywordCount(SearchRequest searchRequest,
                                              @RequestParam(defaultValue = "1") Integer pageNum,
                                              @RequestParam(defaultValue = "10")Integer pageSize){
        return Result.success("关键字搜索文艺作品成功",worksService.queryForListBySearchKeywordCount(searchRequest,pageNum,pageSize));
    }
    @GetMapping("/listRecommendWorks")
    public Result queryForRecommendWorksList(){
        return Result.success("推荐文艺作品列表成功",recommendService.recommendWork());
    }
    @GetMapping("/queryForMyWorks")
    public Result queryForMyWorks(Works works,@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "6") Integer pageSize){
        return Result.success("查询我发布的文艺作品成功",worksService.queryForMyWorks(works,pageNum,pageSize));
    }

}